home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / os2 / edm0407s.zip / STAT3SRC.ZIP / PROCSSTA.CPP < prev    next >
C/C++ Source or Header  |  1996-07-25  |  15KB  |  399 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #include <ithread.hpp>
  5.  
  6. #include "procssta.hpp"
  7. #include "statline.rch"
  8.  
  9. //********************************************************************************
  10. // AProcessStatus :: AProcessStatus - Constructor for status-line                *
  11. //********************************************************************************
  12. AProcessStatus :: AProcessStatus (IWindow * parentWindow) 
  13.                : IStaticText (WND_STATUS_MAIN, parentWindow, parentWindow)
  14. {
  15.   resetAllVariables ();                     // reset all member variables fist
  16.  
  17.   IPaintHandler :: handleEventsFor(this);   // set self as paint handler
  18.  
  19.   AStatusHandler :: handleEventsFor(this);  // set self as status message handler
  20.  
  21.   setAutoDeleteObject();                    // delete C++ object, if PM object is deleted
  22. }
  23.  
  24. //********************************************************************************
  25. // AProcessStatus :: ~AProcessStatus - Destructor for status-line                *
  26. //********************************************************************************
  27. AProcessStatus :: ~AProcessStatus (void) 
  28. {
  29.   delete m_pFont;  // delete the allocated font object
  30. }
  31.  
  32. //********************************************************************************
  33. // AProcessStatus :: resetAllVariables - resets all member variables             *
  34. //********************************************************************************
  35. void AProcessStatus :: resetAllVariables (void) 
  36. {
  37.   m_pFont = 0L;
  38.  
  39.   m_ulTotalTicks =
  40.   m_ulActualTick =
  41.   m_ulPercentReady =
  42.   m_ulPercent =
  43.   m_ulNewSliderSize =
  44.   m_ulOldSliderSize =
  45.   m_ulY = 0L;
  46.  
  47.   m_lStartSliderX =
  48.   m_lStartPercentNumberX =
  49.   m_lEndPercentNumberX =
  50.   m_lWidth =
  51.   m_lResourceId = 0L;
  52.  
  53.   memset (m_szBuffer, 0, sizeof (m_szBuffer));
  54.  
  55.   m_dSizePerTick = 0.0;
  56.   
  57.   m_nPaintWhat = ePaintStd;
  58.  
  59.   m_bProcessIsRunning = false;
  60.  
  61.   m_bPaintSentByMe = false;
  62. }
  63.  
  64. //********************************************************************************
  65. // AProcessStatus :: paintWindow - control member for paint events               *
  66. //********************************************************************************
  67. Boolean AProcessStatus :: paintWindow (IPaintEvent& evt) 
  68. {
  69.   if(m_bPaintSentByMe)          // did I send the paint event?
  70.      {                          // yes                     
  71.       m_bPaintSentByMe = false; // reset the boolean
  72.       if(!m_bProcessIsRunning)  // is the process to indicate still running
  73.          return (true);         // no: return
  74.       switch(m_nPaintWhat)      // the process is running, what is there to do? 
  75.          {
  76.              case ePaintStartProcess:               // do the painting for process's start
  77.                   return (paintStartProcess (evt));
  78.              case ePaintPercentNumber:              // paint the percent number
  79.                   return (paintPercentNumber (evt));
  80.              case ePaintSlider:                     // paint the slider
  81.                   return (paintSlider (evt));
  82.              default:                               // no special painting
  83.                   return (paintStd (evt));
  84.             } 
  85.      }
  86.  
  87.   // the paint event was sent by the system
  88.  
  89.   paintStd (evt);
  90.  
  91.   if(m_bProcessIsRunning)                             // is a process to indicate running?
  92.      {                                                // yes
  93.       paintStartProcess (evt);                        // do the painting as if the process starts
  94.       m_ulOldSliderSize = 0L;                         // reset the slider size
  95.       m_ulActualTick--;                               // because of m_ulActualTick++ in paintStartProcess
  96.       WinPostMsg (handle(), MYM_STATUS_PROCEED, 0, 0);// will call proceed to do the rest of the painting
  97.      }
  98.  
  99.   return (true);
  100. }
  101.  
  102. //********************************************************************************
  103. // AProcessStatus :: paintStd - Std-Paint, sets the background color             *
  104. //********************************************************************************
  105. Boolean AProcessStatus :: paintStd (IPaintEvent& evt) 
  106. {
  107.   IPresSpaceHandle hps ;
  108.  
  109.   hps = evt.presSpaceHandle() ;
  110.  
  111.   GpiSetColor (hps, CLR_PALEGRAY) ; // set the actual colour to pale gray
  112.  
  113.   // move to the lower left corner
  114.  
  115.   m_Point.x = 0 ;
  116.   m_Point.y = 0 ;
  117.  
  118.   GpiMove (hps, &m_Point) ;
  119.  
  120.   // set the point to the upper right corner
  121.  
  122.   m_Point.x = size().width();
  123.   m_Point.y = size().height();
  124.  
  125.   // fill the windows's rectangle with pale gray
  126.  
  127.   GpiBox (hps, DRO_OUTLINEFILL, &m_Point, 0, 0) ;
  128.  
  129.   return (true);
  130. }
  131.  
  132. //********************************************************************************
  133. // AProcessStatus :: paintStartProcess - painting when a process starts          *
  134. //********************************************************************************
  135. Boolean AProcessStatus :: paintStartProcess (IPaintEvent& evt) 
  136. {
  137.   m_nPaintWhat = ePaintStd; // reset m_nPaintWhat
  138.  
  139.   HAB hab = (HAB) IThread :: current().anchorBlock();  // get the anchor block
  140.  
  141.   IPresSpaceHandle hps ;
  142.  
  143.   hps = evt.presSpaceHandle() ; // get the handle to the presentation space
  144.  
  145.   if(!m_pFont) // no font was set
  146.      {
  147.       m_pFont = new IFont (hps); // set the font from this presentation space to get the actual font of the window
  148.       m_ulY = (ULONG) 2 * m_pFont -> maxDescender (); // calculate the y-coordinate of the text
  149.      }
  150.   
  151.   if(!WinLoadString (hab, 0, m_lResourceId, 80, (PSZ) m_szBuffer)) // load the process description from the string table
  152.      strcpy(m_szBuffer, "Process started"); // set the text to a fixed value if WinLoadString failed
  153.  
  154.   GpiSetColor (hps, CLR_BLACK) ; // set the actual colour to black
  155.  
  156.   m_Point.x = 5;      // set the coordinate where to paint the text
  157.   m_Point.y = m_ulY;
  158.  
  159.   GpiCharStringAt(hps, &m_Point, strlen (m_szBuffer), (PCH) m_szBuffer); // paint the text
  160.  
  161.   GpiQueryCurrentPosition (hps, &m_Point); // query the current position
  162.  
  163.   m_lStartSliderX = m_Point.x + 4; // increase it by 4 and set the starting x-coordinate of the slider
  164.  
  165.   sprintf (m_szBuffer, "%lu%%", m_ulPercentReady); // get the percent actually proceeded as a string
  166.  
  167.   m_lWidth = (LONG) m_pFont -> textWidth (m_szBuffer); // calculate the width of this string
  168.  
  169.   // calculate the starting x-coordinate of the percent number
  170.  
  171.   m_lStartPercentNumberX = 
  172.   m_Point.x = (((LONG) size (). width() - m_lStartSliderX - m_lWidth) / 2L) 
  173.               + m_lStartSliderX;
  174.  
  175.   GpiCharStringAt(hps, &m_Point, strlen (m_szBuffer), (PCH) m_szBuffer); // paint the percent number
  176.  
  177.   GpiQueryCurrentPosition (hps, &m_Point); // query the current position
  178.  
  179.   m_lEndPercentNumberX = m_Point.x;        // keep the x-value for later use
  180.  
  181.   // calculate the position and size of the percent number at its hightest value
  182.  
  183.   m_lPercentNumberMaxXRight = ((LONG) size (). width() - m_lStartSliderX - displaySize ("100%"). width () / 2L) 
  184.                                + m_lStartSliderX + displaySize ("100%"). width ();
  185.  
  186.   return (true);
  187. }
  188.  
  189. //********************************************************************************
  190. // AProcessStatus :: paintPercentNumber - refresh the percent number             *
  191. //********************************************************************************
  192. Boolean AProcessStatus :: paintPercentNumber (IPaintEvent& evt) 
  193. {
  194.   m_nPaintWhat = ePaintStd; // reset m_nPaintWhat
  195.  
  196.   IPresSpaceHandle hps;
  197.  
  198.   hps = evt.presSpaceHandle();  // get the handle to the presentation space
  199.  
  200.   // clean up the old percent number
  201.  
  202.   GpiSetColor (hps, CLR_PALEGRAY); // set the actual colour to pale gray, the background colour
  203.  
  204.   // set the point to the upper right corner of the rectangle around the percent number
  205.  
  206.   m_Point.x = m_lEndPercentNumberX;
  207.   m_Point.y = size().height();
  208.  
  209.   GpiMove (hps, &m_Point) ;
  210.  
  211.   // set the point to the lower left corner of the rectangle around the percent number
  212.  
  213.   m_Point.x = m_lStartPercentNumberX;
  214.   m_Point.y = 0L;
  215.  
  216.   GpiBox (hps, DRO_OUTLINEFILL, &m_Point, 0, 0) ; // fill the rectangle
  217.  
  218.   // paint the new percent value
  219.  
  220.   sprintf (m_szBuffer, "%lu%%", m_ulPercentReady); // get the percent as a string
  221.  
  222.   GpiSetColor (hps, CLR_BLACK) ; // set the actual colour to black
  223.  
  224.   m_lWidth = (LONG) m_pFont -> textWidth (m_szBuffer); // calculate the string's size in dots
  225.  
  226.   // calculate the starting x-coordinate of the new percent number
  227.  
  228.   m_lStartPercentNumberX = 
  229.   m_Point.x = (((LONG) size (). width() - m_lStartSliderX - m_lWidth) / 2L) 
  230.               + m_lStartSliderX;
  231.  
  232.   m_Point.y = m_ulY;
  233.  
  234.   GpiCharStringAt(hps, &m_Point, strlen (m_szBuffer), (PCH) m_szBuffer); // display it
  235.  
  236.   GpiQueryCurrentPosition (hps, &m_Point); // query the current position
  237.  
  238.   m_lEndPercentNumberX = m_Point.x; // keep the x-value for later use
  239.  
  240.   return (true);
  241. }
  242.  
  243. //********************************************************************************************
  244. // AProcessStatus :: paintSlider - paints the slider and the percent number inside of it     *
  245. //********************************************************************************************
  246. Boolean AProcessStatus :: paintSlider (IPaintEvent& evt) 
  247. {
  248.   m_nPaintWhat = ePaintStd;  // reset m_nPaintWhat
  249.  
  250.   IPresSpaceHandle hps ;
  251.  
  252.   hps = evt.presSpaceHandle() ;  // get the handle to the presentation space
  253.  
  254.   GpiSetColor (hps, CLR_BLUE) ;  // set the actual colour to blue
  255.  
  256.   // move to upper right corner of the slider
  257.  
  258.   m_Point.x = m_lStartSliderX;
  259.   m_Point.y = 2L;
  260.  
  261.   GpiMove (hps, &m_Point);
  262.  
  263.   // set the point to the lower left end of the slider
  264.  
  265.   m_Point.x = m_lStartSliderX + m_ulNewSliderSize;
  266.   m_Point.y = size (). height () - 2L;
  267.  
  268.   GpiBox (hps, DRO_OUTLINEFILL, &m_Point, 0, 0) ; // paint the slider
  269.  
  270.   if(evt. rect(). right() < m_lStartPercentNumberX) // does the slider overlay (a part of) the percent number?
  271.      return (true); // no ==> return
  272.  
  273.   // set the point to the lower left corner of the percent number
  274.  
  275.   m_Point.x = m_lStartPercentNumberX;
  276.   m_Point.y = m_ulY;
  277.  
  278.   sprintf (m_szBuffer, "%lu%%", m_ulPercentReady); // get the percent number as a string
  279.  
  280.   GpiSetColor (hps, CLR_WHITE) ; // set the actual colour to white
  281.  
  282.   GpiCharStringAt(hps, &m_Point, strlen (m_szBuffer), (PCH) m_szBuffer); // display the overlaid (part of the) percent number
  283.  
  284.   return (true);
  285. }
  286.  
  287. //********************************************************************************
  288. // AProcessStatus :: startProcess - MYM_STATUS_START_PROCESS messages received   *
  289. //********************************************************************************
  290. Boolean AProcessStatus :: startProcess (IEvent& evt) 
  291. {
  292.   m_bProcessIsRunning = true;  // the process is running
  293.  
  294.   m_ulPercent =
  295.   m_ulPercentReady = 0L;
  296.  
  297.   // get the 1. event parameter
  298.  
  299.   PROCESS_START_PARAMS * pStartProcess = ((PROCESS_START_PARAMS *) ((char * ) evt. parameter1 ())); 
  300.  
  301.   if(!pStartProcess -> ulTickSize)    // must be a minimum of 1
  302.      pStartProcess -> ulTickSize = 1L;
  303.  
  304.   m_ulTotalTicks = pStartProcess -> ulProcessSize / pStartProcess -> ulTickSize; // get the total number of ticks
  305.  
  306.   if(!m_ulTotalTicks)       // must also be a minimum of 1
  307.      m_ulTotalTicks = 1L;
  308.  
  309.   m_lResourceId = pStartProcess -> lResourceId; // keep the resource id of the process description
  310.  
  311.   m_ulActualTick = 0L;  // reset the number of ticks actually proceeded
  312.  
  313.   m_nPaintWhat = ePaintStartProcess;  // we want to paint the process's name
  314.  
  315.   m_bPaintSentByMe = true;  // paint event is "selfmade"
  316.  
  317.   // calculate the rect to invalidate
  318.  
  319.   m_Rectl. xLeft = 0L;
  320.   m_Rectl. xRight = size (). width ();
  321.   m_Rectl. yBottom = 0;
  322.   m_Rectl. yTop = size (). height ();
  323.  
  324.   WinInvalidateRect (handle(), &m_Rectl, FALSE);  // send a paint event
  325.  
  326.   m_bPaintSentByMe = false;  // reset the flag
  327.  
  328.   return (true);
  329. }
  330.  
  331. //********************************************************************************
  332. // AProcessStatus :: proceed - MYM_STATUS_PROCEED message received               *
  333. //********************************************************************************
  334. Boolean AProcessStatus :: proceed (IEvent& evt) 
  335. {
  336.   m_ulActualTick++;  // one more tick proceeded
  337.  
  338.   m_dSizePerTick = (double) (size (). width () - m_lStartSliderX) / (double) m_ulTotalTicks;  // calculate the slider size per tick
  339.  
  340.   m_ulNewSliderSize = (ULONG) ((double) m_ulActualTick * m_dSizePerTick);  // get the new slider size
  341.  
  342.   if(m_ulNewSliderSize == m_ulOldSliderSize)  // did the size change?
  343.      return (true);                           // no ==> nothing to do ==> return
  344.  
  345.   m_ulPercent = (m_ulActualTick * (ULONG) 100) / m_ulTotalTicks;  // calculate the percentage proceeded
  346.  
  347.   if(m_ulPercent != m_ulPercentReady)                         // did the percentage change?
  348.      {                                                        // yes
  349.       m_ulPercentReady = m_ulPercent;                         // keep the new percentage proceeded
  350.       m_Rectl. xLeft = m_lStartSliderX + m_ulNewSliderSize;   // set the left end of the rect to invalidate
  351.       if(m_Rectl. xLeft < m_lEndPercentNumberX)               // does the slider overlay the percent number total
  352.         {                                                     // no
  353.          m_nPaintWhat = ePaintPercentNumber;                   // set what we want to paint
  354.          m_bPaintSentByMe = true;                             // paint event is "selfmade"
  355.          m_Rectl. yBottom = 0L;                                // set the rect to invalidate
  356.          m_Rectl. yTop = size (). height ();
  357.          m_Rectl. xRight = m_lPercentNumberMaxXRight;         // largest right-end-x-coordinate of the percent number
  358.          WinInvalidateRect (handle(), &m_Rectl, FALSE);       // send a paint event
  359.          m_bPaintSentByMe = false;                              // reset the flag
  360.        }
  361.      }
  362.  
  363.   m_nPaintWhat = ePaintSlider;                           // set what we want to paint
  364.  
  365.   m_Rectl. xLeft = m_lStartSliderX;                      // set the rect to invalidate
  366.   m_Rectl. xRight = m_lStartSliderX + m_ulNewSliderSize;
  367.   m_Rectl. yBottom = 0L;
  368.   m_Rectl. yTop = size (). height ();
  369.  
  370.   m_ulOldSliderSize = m_ulNewSliderSize;                 // keep the new slider size
  371.  
  372.   m_bPaintSentByMe = true;                               // paint event is "selfmade"
  373.  
  374.   WinInvalidateRect (handle(), &m_Rectl, FALSE);         // send a paint event
  375.  
  376.   m_bPaintSentByMe = false;                              // reset the flag
  377.  
  378.   return (true);
  379. }
  380.  
  381. //********************************************************************************
  382. // AProcessStatus :: endProcess - MYM_STATUS_END_PROCESS message received        *
  383. //********************************************************************************
  384. Boolean AProcessStatus :: endProcess (IEvent& evt) 
  385. {
  386.   m_nPaintWhat = ePaintStd;             // set what we want to paint (clean up the status line)
  387.  
  388.   m_bProcessIsRunning = false;          // the process stopped
  389.  
  390.   m_Rectl. xLeft = 0L;                  // get the window's rect
  391.   m_Rectl. xRight = size (). width ();
  392.   m_Rectl. yBottom = 0;
  393.   m_Rectl. yTop = size (). height ();
  394.  
  395.   WinInvalidateRect (handle(), &m_Rectl, FALSE);  // send a paint event
  396.  
  397.   return (true);
  398. }
  399.